﻿using System;
using System.Globalization;

namespace RTools_NTS.Util
{
	public class IntToken : Token
	{
		public IntToken(int i) : base(0)
		{
			this.obj = i;
		}

		public IntToken(long i) : base(0)
		{
			this.obj = i;
		}

		public IntToken(string s) : base(0)
		{
			this.Parse(s);
		}

		public IntToken(string s, int line) : base(line)
		{
			this.Parse(s);
		}

		public IntToken(int i, int line) : base(line)
		{
			this.obj = i;
		}

		public IntToken(long l, int line) : base(line)
		{
			this.obj = l;
		}

		public static IntToken ParseHex(string s, int lineNumber)
		{
			IntToken result = null;
			try
			{
				result = new IntToken(Convert.ToInt32(s, 16), lineNumber);
			}
			catch
			{
				result = new IntToken(Convert.ToInt64(s, 16), lineNumber);
			}
			return result;
		}

		private void Parse(string s)
		{
			int num;
			if (int.TryParse(s, NumberStyles.Any, NumberFormatInfo.InvariantInfo, out num))
			{
				this.obj = num;
				return;
			}
			long num2;
			if (long.TryParse(s, NumberStyles.Any, NumberFormatInfo.InvariantInfo, out num2))
			{
				this.obj = num2;
				return;
			}
			foreach (int fromBase in new int[]
			{
				16,
				2,
				8
			})
			{
				try
				{
					this.obj = Convert.ToInt32(s, fromBase);
					return;
				}
				catch
				{
					try
					{
						this.obj = Convert.ToInt64(s, fromBase);
						return;
					}
					catch
					{
					}
				}
			}
			this.obj = null;
		}

		public override string ToDebugString()
		{
			if (this.obj != null)
			{
return string.Format("IntToken: {0}", this.obj);
			}
return string.Format("IntToken: null", new object[0]);
		}

		public override string ToString()
		{
			if (this.obj != null)
			{
				return string.Format("{0}", this.obj);
			}
			return string.Format("null", new object[0]);
		}

		public override int GetHashCode()
		{
			return this.ToString().GetHashCode();
		}

		public override string StringValue
		{
			get
			{
				if (this.obj != null)
				{
					return string.Format("{0}", this.obj);
				}
				return string.Format("null", new object[0]);
			}
		}

		public override bool Equals(object other)
		{
			if (other == null)
			{
				return false;
			}
			if (!base.GetType().Equals(other.GetType()))
			{
				return false;
			}
			if (this.obj == null || ((IntToken)other).obj == null)
			{
				return false;
			}
			if (!this.obj.GetType().Equals(((IntToken)other).obj.GetType()))
			{
				return false;
			}
			if (this.obj is int)
			{
				if (((int)this.obj).Equals((int)((IntToken)other).Object))
				{
					return true;
				}
			}
			else if (((long)this.obj).Equals((long)((IntToken)other).Object))
			{
				return true;
			}
			return false;
		}
	}
}
